package com.fs.chatx.module.chat.service;

import cn.hutool.core.lang.UUID;
import com.fs.chatx.module.chat.entity.ChatMessage;
import com.fs.chatx.module.chat.entity.ChatSession;
import com.fs.chatx.module.chat.enums.Constants;
import com.fs.chatx.module.chat.mapper.ChatSessionMapper;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.dromara.easyes.core.conditions.update.LambdaEsUpdateChainWrapper;
import org.dromara.easyes.core.kernel.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ChatSessionService
{
    @Resource
    private ChatSessionMapper chatSessionMapper;



    public String  createChat(ChatSession session)
    {
        String sessionId = UUID.randomUUID().toString();
        session.setCreateTime(LocalDateTime.now());
        session.setSessionId(sessionId);
        session.setStatus(Constants.NORMAL.getCode());
        chatSessionMapper.insert(session);
        return sessionId;
    }


    public void  removeChatSession(String sessionId)
    {
        Wrapper<ChatSession> updateQuery=new LambdaEsQueryWrapper<>(ChatSession.class)
                .eq(ChatSession::getSessionId, sessionId);
        ChatSession session=new ChatSession();
        session.setStatus(Constants.DELETED.getCode());
        session.setUpdateTime(LocalDateTime.now());
        chatSessionMapper.update(session,updateQuery);
    }

    public List<ChatSession> chatList(String userId){
        Wrapper<ChatSession> session=new LambdaEsQueryWrapper<>(ChatSession.class)
                .eq(ChatSession::getUserId, userId)
                .eq(ChatSession::getStatus, Constants.NORMAL.getCode())
                .orderByDesc(ChatSession::getCreateTime)
                ;
        return chatSessionMapper.selectList(session);
    }

}
